/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.gallery;

import com.android.camera.Camera;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import com.android.camera.RephotoProvider;

/**
 * Represents an ordered collection of Image objects. Provides an API to add
 * and remove an image.
 */
public class ImageList extends BaseImageList implements IImageList {

    private static final String TAG = "ImageList";

    private static final String[] ACCEPTABLE_IMAGE_TYPES =
            new String[] { "image/jpeg", "image/png", "image/gif" };

    /**
     * ImageList constructor.
     */
    public ImageList(ContentResolver resolver, Uri imageUri,
            int sort, String bucketId, boolean justOriginal) {
        super(resolver, imageUri, sort, bucketId, justOriginal);
    }

    private static final String WHERE_CLAUSE =
            "(" + Media.MIME_TYPE + " in (?, ?, ?))";
    private static final String WHERE_CLAUSE_WITH_BUCKET_ID =
            WHERE_CLAUSE + " AND " + Media.BUCKET_ID + " = ? ";

    protected String whereClause() {
        String result=mBucketId == null ? WHERE_CLAUSE : WHERE_CLAUSE_WITH_BUCKET_ID;
        result += "AND " + RephotoProvider.IS_ORIGINAL + " = ?";
        return result;
    }

    protected String[] whereClauseArgs() {
        // TODO: Since mBucketId won't change, we should keep the array.
        int count=ACCEPTABLE_IMAGE_TYPES.length + 1;
        if (mBucketId!=null) {
            count++;
        }
        String[] result = new String[count];
        
        int index=ACCEPTABLE_IMAGE_TYPES.length;
        System.arraycopy(ACCEPTABLE_IMAGE_TYPES, 0, result, 0, ACCEPTABLE_IMAGE_TYPES.length);
        if (mBucketId!=null) {
            result[index]=mBucketId;
            index++;
        }
        result[index]= (mJustOriginal ? "1" : "0");
        return result;
    }

    @Override
    protected Cursor createCursor() {
        Log.v(TAG, "Create cursor for "+mBaseUri);
        /*
        Cursor c = Media.query(
                mContentResolver, mBaseUri, IMAGE_PROJECTION,
                whereClause(), whereClauseArgs(), sortOrder());
                */
        Log.w(TAG, "whereClause: "+whereClause());
        for (String s : whereClauseArgs()) {
            Log.w(TAG, "whereClauseArg: "+s);
        }
        
        Cursor c = mContentResolver.query(mBaseUri, IMAGE_PROJECTION, 
                whereClause(), whereClauseArgs(), sortOrder());

        Log.w(TAG, "Number of rows in cursor: "+c.getCount());
        return c;
    }

    public static final String[] IMAGE_PROJECTION = new String[] {
            Media._ID,
            Media.DATE_TAKEN,
            Media.MIME_TYPE,
            Media.ORIENTATION,
            Media.DATA,
            Media.SIZE,
            Media.BUCKET_ID,
            Media.MINI_THUMB_MAGIC,
            Media.DATE_MODIFIED,
            Camera.PROJECT_NAME,
            Media.TITLE,
            Media.DISPLAY_NAME,
            RephotoProvider.IS_ORIGINAL,
            Media.BUCKET_DISPLAY_NAME
    };

    public static final int INDEX_ID = 0;
    public static final int INDEX_DATE_TAKEN = 1;
    public static final int INDEX_MIME_TYPE = 2;
    public static final int INDEX_ORIENTATION = 3;
    public static final int INDEX_DATA = 4;
    public static final int INDEX_SIZE = 5;
    public static final int INDEX_BUCKET_ID = 6;
    public static final int INDEX_MINI_THUMB_MAGIC = 7;
    public static final int INDEX_DATE_MODIFIED = 8;
    public static final int INDEX_PROJECT_NAME = 9;
    public static final int INDEX_TITLE = 10;
    public static final int INDEX_DISPLAY_NAME = 11;
    public static final int INDEX_IS_ORIGINAL = 12;
    public static final int INDEX_BUCKET_DISPLAY_NAME = 13;

    @Override
    protected BaseImage loadImageFromCursor(Cursor cursor) {
        long id = cursor.getLong(INDEX_ID);
        long dateTaken = cursor.getLong(INDEX_DATE_TAKEN);
        if (dateTaken == 0) {
            dateTaken = cursor.getLong(INDEX_DATA) * 1000;
        }
        long miniThumbMagic = cursor.getLong(INDEX_MINI_THUMB_MAGIC);
        int orientation = cursor.getInt(INDEX_ORIENTATION);
        boolean isOriginal = cursor.getInt(INDEX_IS_ORIGINAL)==1;
        String projectName = cursor.getString(INDEX_PROJECT_NAME);
        return new Image(mContentResolver, id,
                contentUri(id), miniThumbMagic, dateTaken,
                orientation, projectName, isOriginal);
    }
}
